<?php
declare (strict_types = 1);

namespace app\common\validate;

use app\common\model\content\Category;
use app\common\model\content\Topic;
use app\common\model\content\Article as ArticleModel;
use think\Validate;

class Article extends Validate
{
    const TAG_NUM = 3;

    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'category_id' => 'require|integer|checkExist',
        'topic_id'    => 'require|integer|checkTopicExist|checkDone',
        'title'       => 'require|max:64',
        'brief'       => 'max:75',
        'image'       => 'max:125|isFile',
        'disallow'    => 'in:0,1',
        'content'     => 'require',
        'tags'        => 'max:64|checkNum|checkLen',
        'status'      => 'in:1,2,3',
        'best'        => 'in:1,2|checkBest|checkUpdateBest',
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */
    protected $message = [
        'category_id.require'  => '所属分类必须',
        'category_id.integer'  => '所属分类无效',
        'category_id.checkExist'  => '所属分类无效',

        'topic_id.require'      => '所属话题必须',
        'topic_id.integer'      => '所属话题无效',
        'topic_id.checkExist'   => '所属话题无效',
        'topic_id.checkDone'    => '话题已完结',

        'title.require' => '标题必须',
        'title.max'     => '标题最多不能超过64个字符',

        'brief.max'     => '简介最多不能超过75个字符',

        'image.max'     => '图片最多不能超过125个字符',
        'image.isFile'      => '请重新选择图片.',

        'disallow.in'     => '可评论错误',

        'content.require'   => '内容必须',

        'tags.max'          => '标签最多不能超过64个字符',
        'tags.checkNum'     => '文章最多关联'.self::TAG_NUM . '个标签',
        'tags.checkLen'     => '标签长度不能超过15个字符',

        'status.in'         => '状态错误',

        'best.in'           => '精品推荐错误',
        'best.checkBest'    => '精品文章请设置图片',
        'best.checkUpdateBest'    => '请重新设置文章图片',

    ];

    //定义场景(创建普通文章 创建话题文章)
    protected $scene = [
        ArticleModel::SCENE_NORMAL  =>  ['title','brief','category_id','image','disallow','content','tags','status','best'],
        ArticleModel::SCENE_TOPIC   =>  ['title','brief','category_id','image','disallow','content','tags','status','best','topic_id']
    ];

    // 自定义验证规则判断分类id是否有效
    protected function checkExist($value)
    {
        return (bool) Category::where(['id'=>$value])->count();
    }
    // 自定义验证规则判断话题id是否有效
    protected function checkTopicExist($value)
    {
        return (bool) Topic::where(['id'=>$value])->count();
    }

    //以逗号分隔 最多不能超过3个
    protected function checkNum($value){
        $str = str_replace('，', ',', cutstr_html($value));
        return self::TAG_NUM >= count(explode(',', $str));
    }
    //检测标签长度
    protected function checkLen($value){
        $arr = explode(',', str_replace('，', ',', cutstr_html($value)));
        foreach ($arr as $item){
            if(strlen($item) > 15) return false;
        }
        return true;
    }
    //检测话题是否已经完结
    protected function checkDone($value){
        return ! Topic::isDone($value);
    }
    //检测选中精品文章后必须有文章封面
    protected function checkBest($value, $rule = null, $data=[]){
        $value = (int) $value;
        if ($value === ArticleModel::BEST_YES && empty($data['image'])){
            return false;
        }
        return true;
    }
    //检测普通文章修改为精品文章后 图片是否是独立图片(有时候是引用文章内容中的图片)
    protected function checkUpdateBest($value, $rule = null, $data=[]){
        $value = (int) $value;
        if ($value === ArticleModel::BEST_YES &&
            strpos($data['image'],'/') === 0){
            return false;
        }
        return true;
    }

    // 自定义验证规则判断分类id是否有效
    protected function isFile($value, $rule, $data=[])
    {
        $file1 = config('filesystem.disks.local.root') . DS . ltrim($value, '\\/');
        $file2 = config('filesystem.disks.public.root') . DS . ltrim($value, '\\/');
        $file3 = config('filesystem.public.root') . DS . ltrim($value, '\\/');
        return is_file($file1) || is_file($file2) || is_file($file3);
    }
}
